const { ref } = Vue
import { useFetch } from '../../../Scripts/hooks/useFetch.js'
const { ElMessage } = ElementPlus

export function useNCProgramView() {
  let ncProgramViewDialogVisible = ref(false);
  let ncProgramDataList = ref([]);//件号NC程序列表
  let ncProgarmResourcesList = ref([]);//设备清单
  let ncProgarmFilesList = ref([]);//附件清单
  let currentRow = ref(null);

  // 获取NC程序
  const getNCProgramData = async configObj => {
    const { currentSelectContainer, baseAPI } = configObj;
    if (!currentSelectContainer.productid) {
      return ElMessage({ type: 'error', message: `请选择批次`, grouping: true });
    }
    ncProgramViewDialogVisible.value = true;
    ncProgramDataList.value = [];// 清空件号NC程序列表
    ncProgarmResourcesList.value = [];// 清空设备清单
    ncProgarmFilesList.value = [];// 清空附件清单
    const { productname, workflowid } = currentSelectContainer;
    const { data } = await useFetch({
      url: `${baseAPI}?actions=getNCProgramData`,
      data: { type: 'getNCProgramData', productname, workflowid }
    });
    ncProgramDataList.value = data.value;
  };

  // 查询NC程序明细
  const getNCProgramDetail = async row => {
    currentRow.value = row;
    const { id } = row;
    ncProgarmResourcesList.value = [];
    ncProgarmFilesList.value = [];
    const { data } = await useFetch({
      url: `../../../Ashx/ContainerStepWorkReportVue3PageHandler.ashx?actions=getNCProgramDetail`,
      data: { type: 'getNCProgramDetail', parentid: id }
    });
    ncProgarmResourcesList.value = data.value.filter(item => item.equip_code);
    ncProgarmFilesList.value = data.value.filter(item => !item.equip_code);
  }

  const tableRowClassName = ({ row }) => {
    if (currentRow.value === row) {
      return 'highlight-row';
    }
    return '';
  }

  return {
    ncProgramViewDialogVisible,
    ncProgramDataList,
    getNCProgramData,
    tableRowClassName,
    ncProgarmResourcesList,//设备清单
    ncProgarmFilesList,//附件清单
    getNCProgramDetail
  };
}